<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linux 高并发优化指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 1.5rem;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            position: relative;
        }
        
        .code-block::before {
            content: attr(data-lang);
            position: absolute;
            top: 0.5rem;
            right: 0.5rem;
            color: #858585;
            font-size: 0.75rem;
            text-transform: uppercase;
        }
        
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 2.5rem;
            height: 2.5rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: bold;
            margin-right: 1rem;
        }
        
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                <i class="fas fa-server mr-4"></i>Linux 高并发优化指南
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto">
                深入探索系统调优的艺术，释放服务器的极限性能
            </p>
            <div class="mt-8 flex justify-center space-x-8 text-lg">
                <div><i class="fas fa-tachometer-alt mr-2"></i>性能提升</div>
                <div><i class="fas fa-network-wired mr-2"></i>连接优化</div>
                <div><i class="fas fa-shield-alt mr-2"></i>稳定可靠</div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                在高并发场景下优化 Linux 服务器的连接数涉及多个方面，包括系统参数配置、应用程序优化以及网络硬件的调优。本指南将带您深入了解每个优化环节，助您构建高性能服务架构。
            </p>
        </div>

        <!-- Optimization Architecture Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-2xl font-bold mb-6 text-center gradient-text">优化架构全景图</h2>
            <div class="mermaid">
                graph TD
                    A[高并发优化] --> B[系统内核参数]
                    A --> C[应用程序优化]
                    A --> D[网络配置]
                    A --> E[监控分析]
                    A --> F[硬件优化]
                    
                    B --> B1[文件描述符]
                    B --> B2[网络核心参数]
                    B --> B3[TCP参数调优]
                    
                    C --> C1[连接池]
                    C --> C2[非阻塞IO]
                    C --> C3[代码优化]
                    
                    D --> D1[负载均衡]
                    D --> D2[TCP连接重用]
                    D --> D3[网络设备优化]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Section 1: System Kernel Parameters -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="section-number">1</span>
                调整系统内核参数
            </h2>
            
            <div class="space-y-6">
                <div class="border-l-4 border-purple-500 pl-6">
                    <h3 class="text-xl font-semibold mb-3 flex items-center">
                        <i class="fas fa-file-alt text-purple-500 mr-3"></i>
                        最大文件描述符数（ulimit）
                    </h3>
                    <p class="text-gray-700 mb-4">增加系统允许的最大文件描述符数，以支持更多的并发连接。</p>
                    <div class="code-block" data-lang="bash">
# 临时设置
ulimit -n 65536

# 永久设置
echo "fs.file-max = 65536" >> /etc/sysctl.conf
sysctl -p</div>
                </div>

                <div class="border-l-4 border-purple-500 pl-6">
                    <h3 class="text-xl font-semibold mb-3 flex items-center">
                        <i class="fas fa-cog text-purple-500 mr-3"></i>
                        调整 net.core 参数
                    </h3>
                    <p class="text-gray-700 mb-4">配置网络核心参数，以优化网络处理性能。</p>
                    <div class="code-block" data-lang="bash">
echo "net.core.somaxconn = 65535" >> /etc/sysctl.conf  # 调整监听队列的最大长度
echo "net.core.netdev_max_backlog = 65536" >> /etc/sysctl.conf  # 调整网络设备的最大等待队列长度
echo "net.ipv4.tcp_max_syn_backlog = 65536" >> /etc/sysctl.conf  # 调整 TCP 连接的 SYN 队列长度
echo "net.ipv4.ip_local_port_range = 1024 65535" >> /etc/sysctl.conf  # 调整可用的本地端口范围
sysctl -p</div>
                </div>

                <div class="border-l-4 border-purple-500 pl-6">
                    <h3 class="text-xl font-semibold mb-3 flex items-center">
                        <i class="fas fa-network-wired text-purple-500 mr-3"></i>
                        调整 net.ipv4.tcp 参数
                    </h3>
                    <p class="text-gray-700 mb-4">优化 TCP 相关的设置以提高连接数和吞吐量。</p>
                    <div class="code-block" data-lang="bash">
echo "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf  # 允许重用 TIME_WAIT 状态的套接字
echo "net.ipv4.tcp_tw_recycle = 1" >> /etc/sysctl.conf  # 启用 TIME_WAIT 状态的回收（注：在某些网络环境中可能不适用）
echo "net.ipv4.tcp_fin_timeout = 30" >> /etc/sysctl.conf  # 调整 TCP FIN 等待超时
sysctl -p</div>
                </div>
            </div>
        </div>

        <!-- Section 2: Application Optimization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="section-number">2</span>
                优化应用程序
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <i class="fas fa-database text-3xl text-purple-600 mb-4"></i>
                    <h3 class="text-lg font-semibold mb-2">连接池配置</h3>
                    <p class="text-gray-700 text-sm">使用数据库和服务的连接池，以减少每个请求创建和销毁连接的开销。例如，在数据库连接中使用连接池库，如 HikariCP 或 Apache DBCP。</p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <i class="fas fa-bolt text-3xl text-blue-600 mb-4"></i>
                    <h3 class="text-lg font-semibold mb-2">非阻塞 I/O</h3>
                    <p class="text-gray-700 text-sm">使用非阻塞 I/O 或异步编程模型来处理高并发请求。例如，在 Java 中使用 Netty、Vert.x 或 Reactive Streams。</p>
                </div>
                
                <div class="bg-gradient-to-br from-green-50 to-teal-50 p-6 rounded-xl">
                    <i class="fas fa-code text-3xl text-green-600 mb-4"></i>
                    <h3 class="text-lg font-semibold mb-2">优化应用程序代码</h3>
                    <p class="text-gray-700 text-sm">确保应用程序代码高效且无内存泄漏，避免阻塞操作和长时间运行的线程。</p>
                </div>
            </div>
        </div>

        <!-- Section 3: Network Configuration -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="section-number">3</span>
                配置和优化网络
            </h2>
            
            <div class="space-y-6">
                <div class="flex items-start">
                    <i class="fas fa-balance-scale text-2xl text-purple-600 mr-4 mt-1"></i>
                    <div class="flex-1">
                        <h3 class="text-xl font-semibold mb-2">负载均衡</h3>
                        <p class="text-gray-700">使用负载均衡器（如 Nginx、HAProxy）来分发流量到多个后端服务器，减少单台服务器的负载。</p>
                    </div>
                </div>
                
                <div class="flex items-start">
                    <i class="fas fa-sync-alt text-2xl text-purple-600 mr-4 mt-1"></i>
                    <div class="flex-1">
                        <h3 class="text-xl font-semibold mb-2">TCP 连接重用</h3>
                        <p class="text-gray-700 mb-4">配置 TCP 连接的复用选项，以减少连接建立和拆除的开销。</p>
                        <div class="code-block" data-lang="bash">
echo "net.ipv4.tcp_keepalive_time = 300" >> /etc/sysctl.conf  # 调整 TCP 保活时间
echo "net.ipv4.tcp_keepalive_probes = 3" >> /etc/sysctl.conf  # 调整 TCP 保活探测次数
sysctl -p</div>
                    </div>
                </div>
                
                <div class="flex items-start">
                    <i class="fas fa-ethernet text-2xl text-purple-600 mr-4 mt-1"></i>